package com.sooncode.project.focusservice.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.sooncode.project.core.annotations.Lookup;
import com.sooncode.project.core.annotations.LookupModel;
import com.sooncode.project.core.finder.Finder;
import com.sooncode.project.core.model.DomainException;
import com.sooncode.project.focusservice._.base.BaseDomainModel;
import com.sooncode.project.focusservice._.base.Utils;
import lombok.Data;

import java.util.*;
import java.util.stream.Collectors;

@Data
@LookupModel
public class Sensory extends BaseDomainModel<Sensory> {
    private String orgId;
    @Lookup(fromModel = Organization.class, localField = "orgId", fromField = "name")
    private String orgName;
    private String memberId;
    @Lookup(fromModel = Member.class, localField = "memberId", fromField = "name")
    private String name;
    @Lookup(fromModel = Member.class, localField = "memberId", fromField = "sex")
    private String sex;
    @Lookup(fromModel = Member.class, localField = "memberId", fromField = "age")
    private String age;
    @Lookup(fromModel = Member.class, localField = "memberId", fromField = "member_no")
    private String memberNo;
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date recordDate;
    private String phone;
    private String teacher;
    private String grade;
    private int item1;
    private int item2;
    private int item3;
    private int item4;
    private int item5;
    private int item6;
    private int item7;
    private int item8;
    private int item9;
    private String emotion; //孩子状态,情绪应对
    private String expression;//语言表达
    private String screening;//感统筛查
    private String evaluate;//综合评价
    private int score1;
    private int score2;
    private int score3;
    private int score4;
    private int score5;
    private int score6;
    private int score7;
    private int score8;
    private int score9;
    private int subjectScore1;
    private int subjectScore2;
    private int subjectScore3;
    private int sumScore;
    private double averageScore;
    private int level1;
    private String level1Name;
    private int level2;
    private String level2Name;
    private int level3;
    private String level3Name;


    @Override
    public void add(Map<String,Object> map){
        try {
            calc(map);
        }catch (Exception ex){
            throw new DomainException("计算评测结果错误！请检查录入的数据是否完整");
        }
        super.add(map);
    }
    @Override
    public void modify(Map<String,Object> map){
        try {
            calc(map);
        }catch (Exception ex){
            throw new DomainException("计算评测结果错误！请检查录入的数据是否完整");
        }
        super.modify(map);
    }

    private void calc(Map<String,Object> map){
        List<LevelHelper> levelList=new ArrayList<>();
        levelList.add(new LevelHelper(1,1,0,54,"较差"));
        levelList.add(new LevelHelper(1,2,55,69,"一般"));
        levelList.add(new LevelHelper(1,3,70,84,"良好"));
        levelList.add(new LevelHelper(1,4,85,100,"优秀"));
        levelList.add(new LevelHelper(2,1,0,49,"较差"));
        levelList.add(new LevelHelper(2,2,50,59,"一般"));
        levelList.add(new LevelHelper(2,3,60,69,"良好"));
        levelList.add(new LevelHelper(2,4,70,100,"优秀"));
        levelList.add(new LevelHelper(3,1,0,49,"较差"));
        levelList.add(new LevelHelper(3,2,50,59,"一般"));
        levelList.add(new LevelHelper(3,3,60,69,"良好"));
        levelList.add(new LevelHelper(3,4,70,100,"优秀"));
        int age=Integer.parseInt(map.get("age").toString());
        if(age<4) age=4;
        if(age>12) age=12;
        List<SensoryScore> scoreList = new Finder<SensoryScore>(SensoryScore.class)
                .byField("age", age).list();
        for(int i=1;i<=9;i++){
            int item=Integer.parseInt(map.get("item"+i).toString());
            final int index=i;
            SensoryScore score = scoreList.stream().filter( f->f.getNo()==index&&f.getMin()<=item&&f.getMax()>=item).findFirst().orElse(null);
            if(score==null) throw new RuntimeException("评分错误！");
            map.put("score"+i,score.getScore());
        }
        int subjectScore1=(int)map.get("score1")+(int)map.get("score2")+(int)map.get("score3");
        int subjectScore2= (int)map.get("score4")+(int)map.get("score5")+(int)map.get("score6");
        int subjectScore3=(int)map.get("score7")+(int)map.get("score8")+(int)map.get("score9");
        int sum=subjectScore1+subjectScore2+subjectScore3;
        map.put("subjectScore1",subjectScore1);
        map.put("subjectScore2", subjectScore2);
        map.put("subjectScore3", subjectScore3);
        map.put("sumScore", sum);
        map.put("averageScore", Utils.doubleFormat2((double)sum/3));
        LevelHelper level1=levelList.stream().filter(f->f.item==1&&f.minScore<=subjectScore1&&f.maxScore>=subjectScore1).findFirst().get();
        map.put("level1", level1.level);
        map.put("level1Name", level1.levelName);
        LevelHelper level2=levelList.stream().filter(f->f.item==2&&f.minScore<=subjectScore2&&f.maxScore>=subjectScore2).findFirst().get();
        map.put("level2", level2.level);
        map.put("level2Name", level2.levelName);
        LevelHelper level3=levelList.stream().filter(f->f.item==3&&f.minScore<=subjectScore3&&f.maxScore>=subjectScore3).findFirst().get();
        map.put("level3", level3.level);
        map.put("level3Name", level3.levelName);

    }
    @Data
    class LevelHelper{
        int item;
        int level;
        int minScore;
        int maxScore;
        String levelName;
        public LevelHelper(int item,int level, int minScore,int maxScore, String levelName) {
            this.item = item;
            this.level = level;
            this.minScore = minScore;
            this.maxScore = maxScore;
            this.levelName = levelName;
        }

    }

}
